package com.android.home.fragment;

import android.view.ViewGroup;
import android.widget.Toast;
import com.android.common.mvp.model.CodeMsgBean;
import com.android.common.base.rv.BaseRvAdapter;
import com.android.common.base.rv.BaseRvFragment;
import com.android.common.base.rv.BaseRvHolder;
import com.android.common.base.rv.BaseTreeRvHolder;
import com.android.common.constant.Constant;
import com.android.common.mvp.model.Node;
import com.android.common.utils.TreeHelper;
import com.android.home.R;
import com.android.home.mvp.present.home.HomePresent;
import java.util.ArrayList;
import java.util.List;

public class VPTreeFragment extends BaseRvFragment<Node<Object>> {

    private HomePresent present;

    private List<Node<Object>> mTreeDataList;

    private List<Node<Object>> mVisibleNodes;

    private final TreeHelper<Object> mTreeHelper = new TreeHelper<Object>();

    public VPTreeFragment() {
    }

    @Override
    protected void onInitView() {
        super.onInitView();
        present = new HomePresent(this);
        mTreeDataList = new ArrayList<>();
    }

    @Override
    protected boolean onNeedBack() {
        return false;
    }

    @Override
    protected boolean onNeedToolBar() {
        return false;
    }

    @Override
    protected BaseRvAdapter<Node<Object>> initAdapter() {
        BaseRvAdapter<Node<Object>> adapter = new BaseRvAdapter<Node<Object>> (this.getContext()) {
            @Override
            protected BaseRvHolder<Node<Object>> createDataViewHolder(ViewGroup parent, int viewType) {
                return new BaseTreeRvHolder<Object>(mLayoutInflater.inflate(R.layout.common_iv_tv, parent, false));
            }
        };
        adapter.setOnItemClickListener((view, position) -> {
            Node<Object> node = mVisibleNodes.get(position);
            if (node.isLeaf()){
                Toast.makeText(mActivity, "this is leaf", Toast.LENGTH_SHORT).show();
            }else {
                node.setExpand(!node.isExpand());
                mVisibleNodes = mTreeHelper.filterVisibleNode(mTreeDataList,position);
                refreshOrLoadSuccess(mVisibleNodes);
            }
        });
        return adapter;
    }


    @Override
    public void onFailed(CodeMsgBean responseBean, String methodName) {
        super.onFailed(responseBean, methodName);
        refreshOrLoadFailed();
    }

    @Override
    protected void onLoadData() {
        super.onLoadData();
        present.getTreeList();
    }

    @Override
    public boolean onEnableRefresh() {
        return false;
    }

    @Override
    public boolean onEnableLoadMore() {
        return false;
    }

    @Override
    public<D> void onSucceed(D d , String methodName) {
        super.onSucceed(d, methodName);
        if (d instanceof Node){
            Node<Object> rootNode = (Node<Object>)d;
            mTreeHelper.convertTreeData2List(mTreeDataList,rootNode,Constant.ZERO,false);
            mVisibleNodes= mTreeHelper.filterVisibleNode(mTreeDataList, Constant.ZERO);
            refreshOrLoadSuccess(mVisibleNodes);
        }
    }
}
